package com.cjh.mybatis12.mapping;


import com.cjh.mybatis12.scriping.LanguageDriver;
import com.cjh.mybatis12.session.Configuration;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

/**
 * @Descripton 映射语句类
 * @Author cjh
 * @Date 2023/5/26 11:09
 */
@NoArgsConstructor
@Getter
@Setter
public class MappedStatement {
    private Configuration configuration;
    /**
     * eg:com.cjh.mybatis03.IUserDao.queryUserInfoById
     */
    private String id;
    private SqlCommandType sqlCommandType;
    private SqlSource sqlSource;
    Class<?> resultType;
    private LanguageDriver languageDriver;
    private List<ResultMap> resultMaps;

    /**
     * step-11 新增方法
     */
    public BoundSql getBoundSql(Object parameterObject){
        // 调用 SqlSource#getBoundSql
        return sqlSource.getBoundSql(parameterObject);
    }

    /**
     *建造者
     */

    public static class Builder{
        private MappedStatement mappedStatement = new MappedStatement();
        public Builder(Configuration configuration,String id,SqlCommandType sqlCommandType,
                       SqlSource sqlSource,Class<?> resultType){
            mappedStatement.configuration = configuration;
            mappedStatement.id= id;
            mappedStatement.sqlCommandType = sqlCommandType;
            mappedStatement.sqlSource = sqlSource;
            mappedStatement.resultType = resultType;
            mappedStatement.languageDriver = configuration.getDefaultScriptingLanguageInstance();
        }

        public MappedStatement build(){
            assert mappedStatement.configuration != null;
            assert mappedStatement.id != null;
            return mappedStatement;
        }
        public String id(){
            return mappedStatement.id;
        }
        public Builder resultMaps(List<ResultMap> resultMaps){
            mappedStatement.resultMaps = resultMaps;
            return this;
        }
    }

}
